#ifndef CC_SWITCH_CASE_GO_H
#define CC_SWITCH_CASE_GO_H

/*** 简介
	1. 受开源工程 http://dunkels.com/adam/pt/ 的启发 
	2. 结合实际应用 嵌入式工程经验 进行了简化
	3. 主要是解决了 裸机环境条件下的 延时，条件等待 问题;
*/

#ifndef RET_OK
#define RET_OK			0 	// 
#endif

#ifndef RET_WAIT
#define RET_WAIT		4 	// 
#endif

#define SW_UNTIL(RecoverNum,Cond) \
do{ \
	State = __LINE__;case __LINE__:{\
		if(!(Cond)){\
			ret = RET_WAIT;\
			goto End;\
		}\
		State = RecoverNum;\
	}\
}while(0);

#define SW_YIELD(RecoverNum) \
do{ \
	YIELD_Flag =1;\
	State = __LINE__;case __LINE__:{\
		if(YIELD_Flag == 1){\
			ret = RET_OK;\
			goto End;\
		}\
		State = RecoverNum;\
	}\
}while(0);

/***  SW_YIELD(RecoverNum) 实现方法2
#define SW_YIELD(RecoverNum) YIELD_Flag=1;\
SW_UNTIL(RecoverNum,YIELD_Flag == 1);
*/

#ifdef CFG_MSTICK_API
#define SW_UNTIL_OT(RecoverNum,Cond,MTICK,OT) \
do{ \
	State = __LINE__;case __LINE__:{\
		if(!(Cond || (Check_TimeOut(MTICK,OT) == RET_OT) )){\
			ret=RET_WAIT;\
			goto End;\
		}\
		State = RecoverNum;\
	}\
}while(0);

#define SW_DLYMS(RecoverNum,MTICK,OT) \
do{ \
	State = __LINE__;case __LINE__:{\
		if(!( Check_TimeOut(MTICK,OT) == RET_OT )){\
			ret=RET_WAIT;\
			goto End;\
		}\
		State = RecoverNum;\
	}\
}while(0);
#endif

/* Code Demo
int PTask_XX(void){
	
	// 关键变量定义为静态变量类型
	static xxx 
	static tMs MTick;
	...
	static uint16_t State = 0;
	uint8_t YIELD_Flag=0;
	int ret = RET_OK;
	
	switch(State){
		case 0:{ // TODO: init 
			
			State++;
		}break;
		case 1:{
			
			...
			// 等待 xxx_condition 条件成立
			SW_UNTIL(1,xxx_condition);
			...
			SW_DLYMS(1,MTick,1000);// 延时1秒
			...
			
			State++;
		}break;
		case 2:{
			...
			SW_YIELD(2); // 
			...
			
		}break;
		default:{
			
		}
	}
	
	End:
	// TODO: 
	
	return ret;
}
*/

#endif